<template>
  <div class="container">
    <el-dialog
      :title="
        dialog.dialogType === 'edit' ? '修改费用项目单' : '创建费用项目单'
      "
      :visible.sync="dialog.dialogVisible"
      width="800px"
      center
      :lock-scroll="true"
      @open="getData"
      @close="hideDialog"
    >
      <el-form label-width="100px" label-position="left">
        <el-form-item label="费用名称：">
          <el-input v-model="formData.type_name" placeholder="请输入费用名称" />
        </el-form-item>
        <el-form-item label="费用编码：">
          <el-input v-model="formData.type_code" placeholder="请输入费用编码" />
        </el-form-item>
        <el-form-item label="是否收账：">
          <el-switch
            v-model="formData.is_account"
            active-text="是"
            inactive-text="否"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input
            type="textarea"
            :rows="2"
            v-model="formData.notes"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { indexItem, addItem, editItem } from "@/api/expenseDoc/expenseItems";
export default {
  props: {
    dialog: Object
  },
  data() {
    return {
      formData: {
        type_code: "",
        type_name: "",
        is_account: false,
        notes: ""
      }
    };
  },

  methods: {
    getData() {
      if (this.dialog.dialogType == "edit") {
        this.formData = {
          type_code: this.dialog.formData.type_code,
          type_name: this.dialog.formData.type_name,
          is_account: this.dialog.formData.is_account == 1 ? true : false,
          notes: this.dialog.formData.notes
        };
      }
    },
    hideDialog() {
      this.formData = {
        type_code: "",
        type_name: "",
        is_account: false,
        notes: ""
      };
      this.dialog.dialogVisible = false;
    },
    //提交
    confirm() {
      let data = {
        type_code: this.formData.type_code,
        type_name: this.formData.type_name,
        is_account: this.formData.is_account ? 1 : 2,
        notes: this.formData.notes
      };
      if (this.dialog.dialogType == "edit") {
        data.id = this.dialog.id;
        editItem(data).then(res => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: res.msg || res.message
            });
            this.$emit("upData");
            this.hideDialog();
          }
        });
      } else {
        addItem(data).then(res => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: res.msg || res.message
            });
            this.$emit("upData");
            this.hideDialog();
          }
        });
      }
    },
    //取消
    cancel() {
      this.hideDialog();
    }
  }
};
</script>
